//
//  XXBaseViewController.swift
//  APP
//
//  Created by Rdxer on 2017/12/6.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import Async
 
public enum XXJumpType {
    case navigation
    case present
}

 
public extension UIViewController{
   
    /// type def= present
    func xx_setBackButtomToNavigationBar(imageName:String,type:XXJumpType = .present) {
        xx_setBackButtomToNavigationBar(image: imageName.xx_toImage(), type: type)
    }
    
    func xx_setBackButtomToNavigationBar(image:UIImage,type:XXJumpType = .present) {
        let btn = UIButton(type: UIButton.ButtonType.system)
        btn.setImage(image, for: .normal)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        btn.rx.tap.subscribeOnNext(with: self) { sf, _ in
            switch type {
                case .present:
                    sf.dismiss(animated: true)
                case .navigation:
                    sf.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    func xx_setRightButtomToNavigationBar(title:String? = nil,image:UIImage? = nil,callback:@escaping ()->()) {
        
        let btn = UIButton(type: UIButton.ButtonType.system)
        
        if let title = title {
            btn.setTitle(title, for: .normal)
        }
        if let image = image {
            btn.setImage(image, for: .normal)
        }
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn)
        btn.rx.tap.subscribeOnNext(with: self) { sf, _ in
            callback()
        }
    }
}


@objc
@objcMembers
open class XXBaseViewController: UIViewController {
    
    deinit {
        XXLoger.w(self.classForCoder.description() + "已释放" )
    }
      
    
    open var workerView:UIView!
    
    
    // MARK: - backgroundImageView
    open lazy var backgroundImageView = UIImageView.init()
    open var backgroundImage:UIImage?{
        didSet{
            if backgroundImage != nil{
                backgroundImageView.isHidden = false
                backgroundImageView.image = backgroundImage
                self.view.insertSubview(backgroundImageView, at: 0)
                backgroundImageView.snp.remakeConstraints { make in
                    make.edges.equalToSuperview()
                }
            }else{
                backgroundImageView.image = backgroundImage
                backgroundImageView.isHidden = true
            }
        }
    }
    
  
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        if self.view.backgroundColor == nil {
            self.view.backgroundColor = FastComponentStyle.style.backgroundColor
        }
        prepareViewController()
        workerView = prepareView()
        prepareWorkerView(workerView: workerView)
        
        Async.main {
            XXLoger.w("已创建" + self.classForCoder.description() )
        }
    }
    dynamic
    open func prepareViewController(){
        
    }
    dynamic
    open func prepareView()->UIView{
        return view
    }
    dynamic
    open func prepareWorkerView(workerView:UIView){
        
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    open  override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
}

